package com.rzj.sys_lab_assistant.controller;

import com.rzj.sys_lab_assistant.entity.RecruitmentPost;
import com.rzj.sys_lab_assistant.entity.Technician;
import com.rzj.sys_lab_assistant.entity.bo.RecruitmentPlanBO;
import com.rzj.sys_lab_assistant.entity.bo.RecruitmentPostBO;
import com.rzj.sys_lab_assistant.service.IRecruitmentPlanService;
import com.rzj.sys_lab_assistant.service.IRecruitmentPostService;
import com.rzj.sys_lab_assistant.utils.ResultsUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * @program: sys_lab_assistant
 * @description:
 * @author: 作者
 * @create: 2021-07-01 20:29
 */
@Controller
@RequestMapping("recruitmentPost")
public class RecruitmentPostController {

    @Autowired
    IRecruitmentPostService recruitmentPostService;
    @Autowired
    IRecruitmentPlanService recruitmentPlanService;

    private ResultsUtils resultsUtils;

    @RequestMapping("toAdd")
    public String toAdd() {
        return "recruitmentPost/recruitmentPostAdd";
    }

    @RequestMapping("toEdit/{postId}")
    public String toEdit(Model model, @PathVariable("postId") int postId) {
        model.addAttribute("post", recruitmentPostService.getRecruitmentPostById(postId));
        return "recruitmentPost/recruitmentPostUpdate";
    }

    @RequestMapping("recruitmentPostListByAccount")
    public String recruitmentPostListByAccount(Model model, HttpSession session) { // 技术员页面的岗位信息
        Technician technician = (Technician) session.getAttribute("technician");
        model.addAttribute("postList", recruitmentPostService.recruitmentPostsListByAccount(technician.getAccount())); // 获取登录的session.technician.account
        return "recruitmentPost/recruitmentPostByAccountList";
    }

    @RequestMapping("recruitmentPostInterviewList")
    public String recruitmentPostInterviewList(Model model, HttpSession session) { // 技术员页面的岗位信息
        Technician technician = (Technician) session.getAttribute("technician");
        model.addAttribute("postList", recruitmentPostService.recruitmentPostsListByAccount(technician.getAccount())); // 获取登录的session.technician.account
        return "recruitmentPost/recruitmentPostInterviewList";
    }

    @RequestMapping("recruitmentPostList")
    public String recruitmentPostList(Model model) { // 学生页面的岗位信息
        model.addAttribute("postList", recruitmentPostService.recruitmentPostsList());
        return "recruitmentPost/recruitmentPostList";
    }

    @RequestMapping("recruitmentPostAdd")
    @ResponseBody
    public Map<String, String> recruitmentPostAdd(@RequestParam("planId") String planId) {
        RecruitmentPlanBO recruitmentPlanBO = recruitmentPlanService.getRecruitmentPlanById(Integer.valueOf(planId));
        System.out.println(recruitmentPlanBO.toString());
        RecruitmentPost recruitmentPost = new RecruitmentPost(0, recruitmentPlanBO.getPlanName(), recruitmentPlanBO.getRequirement(), recruitmentPlanBO.getPlanId());

        int flag = recruitmentPostService.addRecruitmentPost(recruitmentPost);
        if (flag == 1) {
            return resultsUtils.resultsMap("success", "添加成功！");
        } else {
            return resultsUtils.resultsMap("error", "添加失败，请联系管理员！");
        }

    }

    @RequestMapping("recruitmentPostEdit")
    @ResponseBody
    public Map<String, String> recruitmentPostEdit(@RequestParam("postId") String postId,
                                                   @RequestParam("planName") String planName,
                                                   @RequestParam("postContent") String postContent,
                                                   @RequestParam("planId") String planId) {
        RecruitmentPost recruitmentPost = new RecruitmentPost(Integer.valueOf(postId), planName, postContent, Integer.valueOf(planId));

        int flag = recruitmentPostService.updateRecruitmentPost(recruitmentPost);
        if (flag == 1) {
            return resultsUtils.resultsMap("success", "修改成功！");
        } else {
            return resultsUtils.resultsMap("error", "修改失败，请联系管理员！");
        }

    }

    @RequestMapping("recruitmentPostDel")
    @ResponseBody
    public Map<String, String> recruitmentPostDel(@RequestParam("postId") String postId) {

        int flag = recruitmentPostService.delRecruitmentPost(Integer.valueOf(postId));
        if (flag == 1) {
            return resultsUtils.resultsMap("success", "删除成功！");
        } else {
            return resultsUtils.resultsMap("error", "删除失败，请联系管理员！");
        }
    }

    @RequestMapping("recruitmentPostCount")
    @ResponseBody
    public Map<String, String> recruitmentPostCount(@RequestParam("postId") String postId) {

        RecruitmentPostBO recruitmentPost = recruitmentPostService.getRecruitmentPostById(Integer.valueOf(postId));

        int count = recruitmentPostService.countRecruitmentPosts(Integer.valueOf(postId));

        if (recruitmentPost.getPostNum() != count) {
            return resultsUtils.resultsMap("success", "当前可报名！");
        } else {
            return resultsUtils.resultsMap("error", "报名人数已满！");
        }

    }


}
